﻿using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.Modules;
using Abp.Reflection.Extensions;
using EasyCaching.Core;
using Hicap.AuthorizationServer.Application;
using Hicap.AuthorizationServer.Core;
using Hicap.AuthorizationServer.Core.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace Hicap.AuthorizationServer.WebApi
{
    [DependsOn(
        typeof(AuthorizationServerApplicationModule),
        typeof(AuthorizationServerCoreModule),
        typeof(AbpAspNetCoreModule))]
    public class AuthorizationServerWebApiModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public AuthorizationServerWebApiModule(IHostingEnvironment env)
        {
            _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName);
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(AuthorizationServerWebApiModule).GetAssembly());
            IocManager.Register<IEasyCachingKeyGenerator, DefaultEasyCachingKeyGenerator>();
        }

        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(AuthorizationServerConsts.ConnectionStringName);

            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(
                    typeof(AuthorizationServerApplicationModule).GetAssembly()
                );
        }
    }
}